<?php
namespace app\wx\controller;

class wxpay
{
    public function jsapi(){
        $AppId = 'wx5c6558a180573edf';
        $AppSecret = 'eefa0a38149a9637d3c6948d8147a245';
        $key = '9157e3a3e5c147b84d1b6f6b48f823e5';
        $mch_id = '1558864601';
        //通过code获得openid
        if (!isset($_GET['code'])){
            //触发微信返回code码
            $baseUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $urlObj["appid"] = $AppId;
            $urlObj["redirect_uri"] = "$baseUrl";
            $urlObj["response_type"] = "code";
            $urlObj["scope"] = "snsapi_base";
            $urlObj["state"] = "STATE"."#wechat_redirect";
            $bizString = http_build_query($urlObj);
            $baseUrl =  "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
            Header("Location: $baseUrl");
            exit();
        } else {
            //获取code码，以获取openid
            $code = $_GET['code'];
            $urlObj["appid"] = $AppId;
            $urlObj["secret"] = $AppSecret;
            $urlObj["code"] = $code;
            $urlObj["grant_type"] = "authorization_code";
            $bizString = http_build_query($urlObj);
            $config['ua'] = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/";//.$curlVersion['version']." ".$config->GetMerchantId()
            $config['url'] = "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
            $res = curl($config);
            $res = json_decode($res,true);
            dump($res);
            empty($res['openid'])&&tip(1,'openid error',$res);
            $input=[
                'appid'=>$AppId,
                'mch_id'=>$mch_id,
                'nonce_str'=>md5(rand(10000,99999)),
                'sign_type'=>'HMAC-SHA256',
                'body'=>'test',
                'attach'=>'test',
                'out_trade_no'=>date("YmdHis"),
                'total_fee'=>1,
                'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
                'time_start'=>date("YmdHis"),
                'time_expire'=>date("YmdHis",time()+600),
                'notify_url'=>'http://admin.taoyuanapartment.com/wx/notify/wxgzh',
                'trade_type'=>'JSAPI',
                'goods_tag'=>'WXG',
                'product_id'=>date("YmdHis"),
                'openid'=>$res['openid'],
            ];
            $input['sign'] = MakeSign($input,$key);
            dump($input);
            $xml = ArrToXml($input);
            $config['ua'] = "WXPaySDK/ (".PHP_OS.") PHP/".PHP_VERSION;
            $config['url'] = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            $config['data'] = $xml;
            $res = curl($config);
            libxml_disable_entity_loader(true);
            $arr = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            dump($arr);
            if($arr['sign']!=MakeSign($arr,$key)){
                tip('1','签名错误');
            }
            $jsapi = [
                'appId'=>$arr['appid'],
                'nonceStr'=>md5(rand(10000,99999)),
                'package'=>"prepay_id=".$arr['prepay_id'],
                'signType'=>'HMAC-SHA256',
                'timeStamp'=>time(),
            ];
            $jsapi['paySign']=MakeSign($jsapi,$key);
            dump($jsapi);
            $obj = json_encode($jsapi);
            echo "
            <script type=\"text/javascript\">
                if (typeof WeixinJSBridge == \"undefined\"){
                    if( document.addEventListener ){
                     document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                    }else if (document.attachEvent){
                     document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                     document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                    }
                }else{
                     jsApiCall();
                }
                function jsApiCall(){
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',
                        $obj,
                        function(res){
                            WeixinJSBridge.log(res.err_msg);
                            alert(res.err_code+res.err_desc+res.err_msg);
                        }
                    );
                }
            </script>";
        die;
        }
    }
    public function Native(){
        $AppId = 'wx5c6558a180573edf';
        $AppSecret = 'eefa0a38149a9637d3c6948d8147a245';
        $key = '9157e3a3e5c147b84d1b6f6b48f823e5';
        $mch_id = '1558864601';
        $input=[
            'appid'=>$AppId,
            'mch_id'=>$mch_id,
            'nonce_str'=>md5(rand(10000,99999)),
            'sign_type'=>'HMAC-SHA256',
            'body'=>'test',
            'attach'=>'test',
            'out_trade_no'=>date("YmdHis"),
            'total_fee'=>1,
            'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
            'time_start'=>date("YmdHis"),
            'time_expire'=>date("YmdHis",time()+600),
            'notify_url'=>'http://paysdk.weixin.qq.com/notify.php',
            'trade_type'=>'NATIVE',
            'product_id'=>date("YmdHis"),
        ];
        $input['sign'] = MakeSign($input,$key);
        dump($input);
        $xml = ArrToXml($input);
        $config['ua'] = "WXPaySDK/ (".PHP_OS.") PHP/".PHP_VERSION;
        $config['url'] = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $config['data'] = $xml;
        $res = curl($config);
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        dump($arr);
    }


    public function check(){
        $AppId = 'wx5c6558a180573edf';
        $AppSecret = 'eefa0a38149a9637d3c6948d8147a245';
        $key = '9157e3a3e5c147b84d1b6f6b48f823e5';
        $mch_id = '1558864601';
        $input=[
            'appid'=>$AppId,
            'mch_id'=>$mch_id,
            'nonce_str'=>md5(rand(10000,99999)),
            'sign_type'=>'HMAC-SHA256',
            'out_trade_no'=>'20191215125018',
        ];
        $input['sign'] = MakeSign($input,$key);
        dump($input);
        $xml = ArrToXml($input);
        $config['ua'] = "WXPaySDK/ (".PHP_OS.") PHP/".PHP_VERSION;
        $config['url'] = "https://api.mch.weixin.qq.com/pay/orderquery";
        $config['data'] = $xml;
        $res = curl($config);
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        dump($arr);
//        array (size=21)
//  'return_code' => string 'SUCCESS' (length=7)
//  'return_msg' => string 'OK' (length=2)
//  'appid' => string 'wx5c6558a180573edf' (length=18)
//  'mch_id' => string '1558864601' (length=10)
//  'nonce_str' => string 'JLh5G6sUNnLoG3u4' (length=16)
//  'sign' => string '65F7ABAEA0A3F98677F81F1D8E56AC026E71FF467171FEA4CA6FC29379E760E8' (length=64)
//  'result_code' => string 'SUCCESS' (length=7)
//  'openid' => string 'oPcHts09eGE37JdN2eEo9Dy76kL0' (length=28)
//  'is_subscribe' => string 'Y' (length=1)
//  'trade_type' => string 'JSAPI' (length=5)
//  'bank_type' => string 'CITIC_CREDIT' (length=12)
//  'total_fee' => string '1' (length=1)
//  'fee_type' => string 'CNY' (length=3)
//  'transaction_id' => string '4200000448201912159845571931' (length=28)
//  'out_trade_no' => string '20191215125018' (length=14)
//  'attach' => string 'test' (length=4)
//  'time_end' => string '20191215125023' (length=14)
//  'trade_state' => string 'SUCCESS' (length=7)
//  'cash_fee' => string '1' (length=1)
//  'trade_state_desc' => string '支付成功' (length=12)
//  'cash_fee_type' => string 'CNY' (length=3)
    }

}